package com.dmlll.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.dmlll.VO.ProviderOutputVO;
import com.dmlll.VO.PurchaseVO;
import com.dmlll.common.DataGridView;

import com.dmlll.common.ResultObj;
import com.dmlll.domain.*;
import com.dmlll.service.*;

import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-04-12
 */
@RestController
@RequestMapping("/providerOutputDetail")
public class ProviderOutputDetailController {
    @Autowired
    private ProviderOutputDetailService providerOutputDetailService;
    @Autowired
    private ProviderOutputService providerOutputService;
    @Autowired
    private DrugMessageService drugMessageService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseDrupDetailService purchaseDrupDetailService;
    @Autowired
    private HospitalService hospitalService;
    @RequestMapping("getAllOutput")
    public DataGridView getAllOutput(ProviderOutputDetail providerOutputDetail, ProviderOutputVO providerOutput, PurchaseVO purchase, DrugMessage drugMessage){
        //查询退货单
        QueryWrapper<ProviderOutput> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("provider_id",providerOutput.getProviderId());
        queryWrapper.eq("output_form_state",2);//已提交的结算单
        queryWrapper.like(StringUtils.isNotBlank(providerOutput.getOutputNum()),"output_num",providerOutput.getOutputNum());
        queryWrapper.like(StringUtils.isNotBlank(providerOutput.getOutputName()),"output_name",providerOutput.getOutputName());
        queryWrapper.ge(providerOutput.getStartTime()!=null,"create_out_time",providerOutput.getStartTime());
        queryWrapper.le(providerOutput.getEndTime()!=null,"create_out_time",providerOutput.getEndTime());
        List<ProviderOutput> providerOutputList = providerOutputService.list(queryWrapper);
        //获取退货单编号集合
        List<String> outputNumList = new ArrayList<>();
        for(ProviderOutput output : providerOutputList){
            outputNumList.add(output.getOutputNum());//存在则有退货单
        }
        if(outputNumList.size()>0) {
            //获得退货单详情
            QueryWrapper<ProviderOutputDetail> qw = new QueryWrapper<>();
            qw.in("output_num", outputNumList)
                    .eq("output_state",providerOutputDetail.getOutputState());
            List<ProviderOutputDetail> list = providerOutputDetailService.list(qw);
            List<ProviderOutputDetail> resultList = new ArrayList<>();
            //填充数据
            for(ProviderOutputDetail pod:list){
                QueryWrapper<DrugMessage> w1 = new QueryWrapper<>();
                w1.eq("id",pod.getDrugMessageId())
                        .like(StringUtils.isNotBlank(drugMessage.getSerialNumber()),"serial_number",drugMessage.getSerialNumber())
                        .like(StringUtils.isNotBlank(drugMessage.getCommonName()),"common_name",drugMessage.getCommonName())
                        .like(StringUtils.isNotBlank(drugMessage.getDosageForm()),"dosage_form",drugMessage.getDosageForm())
                        .like(StringUtils.isNotBlank(drugMessage.getSize()),"size",drugMessage.getSize())
                        .like(StringUtils.isNotBlank(drugMessage.getUnit()),"unit",drugMessage.getUnit())
                        .eq(drugMessage.getFactor()!=null,"factor",drugMessage.getFactor())
                        .like(StringUtils.isNotBlank(drugMessage.getProductionName()),"production_name",drugMessage.getProductionName())
                        .like(StringUtils.isNotBlank(drugMessage.getCommodityName()),"commodity_name",drugMessage.getCommodityName())
                        .eq(drugMessage.getDrugCategory()!=null&&drugMessage.getDrugCategory()>0,"drug_category",drugMessage.getDrugCategory())
                        .eq(drugMessage.getQuality()!=null&&drugMessage.getQuality()>0,"quality",drugMessage.getQuality());
                pod.setDrugMessage(drugMessageService.getOne(w1));
                QueryWrapper<Purchase> w2 = new QueryWrapper<>();
                w2.eq("id",providerOutput.getProviderId())
                        .eq(purchase.getHospitalId()!=null,"hospital_id",purchase.getHospitalId())
                        .ge(purchase.getCreateTime()!=null,"create_time",purchase.getCreateTime())
                        .le(purchase.getOverTime()!=null,"over_time",purchase.getOverTime())
                        .like(StringUtils.isNotBlank(purchase.getPurchaseNum()),"purchase_num",purchase.getPurchaseNum())
                        .like(StringUtils.isNotBlank(purchase.getPurchaseName()),"purchase_name",purchase.getPurchaseName());
                Purchase purchase1 = purchaseService.getOne(w2);
                if(purchase1!=null) {
                    purchase1.setHospitalName(hospitalService.getById(purchase1.getHospitalId()).getHospitalName());
                }
                pod.setPurchase(purchase1);
                pod.setProvider(providerService.getById(providerOutput.getProviderId()));

                pod.setProviderOutput(providerOutputService.getOne(new QueryWrapper<ProviderOutput>().
                        eq("output_num",pod.getOutputNum())));
                pod.setPurchaseDrupDetail(purchaseDrupDetailService.getOne(new QueryWrapper<PurchaseDrupDetail>().
                        eq("drug_message_id",pod.getDrugMessageId()).
                        eq("purchase_id",pod.getPurchaseId())));
                if(pod.getDrugMessage()!=null&&pod.getPurchase()!=null){
                    resultList.add(pod);
                }
            }
            return new DataGridView(Long.valueOf(resultList.size()),resultList);
        }
        return new DataGridView();
    }
    @RequestMapping("update")
    public ResultObj update(String output){
        try {
            providerOutputDetailService.updateBatchById(JSON.parseArray(output,ProviderOutputDetail.class));
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;
        }
        return ResultObj.UPDATE_SUCCESS;
    }
}

